﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library;
using ZFS.Library.Helper;
using ZFS.Model;
using ZFSData;

namespace ZFS.ServerLibrary
{
    public partial class BaseService : IBaseService
    {
        /// <summary>
        /// 获取字典
        /// </summary>
        /// <returns></returns>
        public byte[] GetDictionarys()
        {
            try
            {
                var dicList = Factory.GetDictionaryTypeManager().GetDictionaryTypes();
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = dicList });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("获取字典", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 查询字典数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public byte[] GetModelsByDic(byte[] search)
        {
            try
            {
                var DicSearch = ZipTools.DecompressionObject(search) as tb_Dictionary;
                var DicList = Factory.GetDictionaryManager().GetModels(DicSearch);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = DicList });
                return bytes;

            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("查询字典数据", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 查询字典数据-分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="search"></param>
        /// <param name="Asc"></param>
        /// <returns></returns>
        public byte[] GetPagingModelsByDic(int pageIndex, int pageSize,
            byte[] search,
            bool Asc = false)
        {
            try
            {
                int totalCount;
                var DicSearch = ZipTools.DecompressionObject(search) as tb_Dictionary;
                var DicList = Factory.GetDictionaryManager().GetPagingModels(pageIndex, pageSize, out totalCount, DicSearch, Asc);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = DicList, TotalCount = totalCount });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("查询字典数据-分页", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }

        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] DeleteEntityByDic(byte[] entity)
        {
            try
            {
                var dic = ZipTools.DecompressionObject(entity) as tb_Dictionary;
                bool result= Factory.GetDictionaryManager().DeleteEntity(dic);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("删除字典", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 更新字典
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] UpdateEntityByDic(byte[] entity)
        {
            try
            {
                var dic = ZipTools.DecompressionObject(entity) as tb_Dictionary;
                bool result= Factory.GetDictionaryManager().UpdateEntity(dic);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("更新字典", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }
       
        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] AddEntityByDic(byte[] entity)
        {
            try
            {
                var dic = ZipTools.DecompressionObject(entity) as tb_Dictionary;
                var NewDic = Factory.GetDictionaryManager().AddEntity(dic);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = NewDic });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("新增字典", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 检查字典是否存在
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] ExistEntityByDic(byte[] entity)
        {
            try
            {
                var dic = ZipTools.DecompressionObject(entity) as tb_Dictionary;
                bool result= Factory.GetDictionaryManager().ExistEntity(dic);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("检查字典是否存在", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }
    }
}
